﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace ViewMaker.Core.Controls
{
    /// <summary>
    /// データグリッド テキスト列
    /// </summary>
    public class DataGridTextColumnViewElement : DataGridColumnViewElement
    {
        public new class Properties : DataGridColumnViewElement.Properties
        {
            public const string StringFormat = "StringFormat";
            public const string IsEmptyAsNull = "IsEmptyAsNull";
            public const string TextAlignment = "TextAlignment";
        }

        /// <summary>
        /// 出力フォーマット
        /// </summary>
        public string StringFormat { get; set; }

        /// <summary>
        /// 空文字をnull扱いするか
        /// </summary>
        [DefaultValue(false)]
        public bool IsEmptyAsNull { get; set; }

        /// <summary>
        /// テキスト配置位置
        /// </summary>
        [Display(Name = "Text Align", Description = "Text Alignment", GroupName = LayoutGroup)]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "HorizontalAlignmentList")]
        public LayoutHorizontalAlignment? TextAlignment { get; set; }

        public DataGridTextColumnViewElement()
        {
            ControlType = ViewControlType.DataGridTextColumn;
        }
        public override List<XamlNode> CreateXaml(XamlNode parent)
        {
            var list = new List<XamlNode>();
#if !SILVERLIGHT
            var col = parent.CreateElement("DataGridTextColumn");
#else
            var col = parent.CreateElement("sdk", "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk", "DataGridTextColumn");
#endif
            if (IsReadOnly) col.SetAttribute("IsReadOnly", IsReadOnly.ToString());

            if (!string.IsNullOrEmpty(Item.BindPath))
            {
                var bindData = Item.BindPath;
                if (!string.IsNullOrEmpty(StringFormat)) bindData += ", StringFormat=" + GetStringFormat(StringFormat);
                if (IsEmptyAsNull) bindData += ", TargetNullValue=''";
                SetBindingAttribute(col, "Binding", bindData, Item.IsOneWay);
            }
#if SILVERLIGHT
            else
            {
                //バインドを空にするとエラーになるため回避策
                SetBindingAttribute(col, "Binding", "__dummy__", Item.IsOneWay);
            }
#endif
            if (TextAlignment.HasValue)
            {
                AddGridElementStyle(col, "DataGridTextColumn.ElementStyle", "TextBlock");
                AddGridElementStyle(col, "DataGridTextColumn.EditingElementStyle","TextBox");
            }
            ApplyBaseProperty(col);
            list.Add(col);
            return list;
        }

        private void AddGridElementStyle(XamlNode col, string type, string targetElement)
        {
#if !SILVERLIGHT
            var eleStyle = col.AddElement(type);
#else
            var eleStyle = col.CreateElement("sdk", "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk", type);
            col.AppendChild(eleStyle);
#endif
            var style = eleStyle.AddElement("Style");
            style.SetAttribute("TargetType", targetElement);
            var setter = style.AddElement("Setter");
            setter.SetAttribute("Property", "TextAlignment");
            setter.SetAttribute("Value", TextAlignment.ToString());
        }
    }

}
